import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Clipboard } from '@angular/cdk/clipboard';
import { NzMessageService } from 'ng-zorro-antd/message';
import { FinanceDrawbackService } from '../finance-drawback/finance-drawback.service';

@Component({
  selector: 'app-finance-drawback-detail',
  templateUrl: './finance-drawback-detail.component.html',
  styleUrls: ['./finance-drawback-detail.component.scss']
})
export class FinanceDrawbackDetailComponent implements OnInit {
  @ViewChild('flowRecord', { static: false }) flowRecord: any;

  routerParamas: any = {};
  detailsData: any = {};

  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  tableData: any = [];

  constructor(
    private clipboard: Clipboard,
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private services: FinanceDrawbackService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routerParamas = res || {};
    })
  }

  ngOnInit(): void {
    this.loadDetailData();
    this.loadFollowUpTable();
  }

  // 查询详情
  async loadDetailData() {
    try {
      const res: any = await this.services.getDrawbackDetail({ id: this.routerParamas.id });
      
      if (res.code == 0) {
        this.detailsData = res.data;
      } else {
        this.message.error(res.message || '详情获取失败');
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  // 查看流水记录
  openModal() {
    this.flowRecord.isRecordVisible = true;
  }

  // 跟进记录tabel
  async loadFollowUpTable() {
    try {
      let params = {
        type: 6, // 1、订单 2、售后 3、客户 4、出库 5、直降 6、退税
        relationInformation: this.routerParamas.id,
        page: this.tableParams.page,
        pageSize: this.tableParams.pageSize
      }
      this.tableParams.tableLoading = true;
      const res: any = await this.services.getFollowRecord(params);
      this.tableParams.tableLoading = false;
      
      if (res.code == 0) {
        const { records, total }: any = res.data;
        this.tableData = records;
        this.tableParams.total = total;
      } else {
        this.message.error(res.message || '跟进记录获取失败');
      }
    } catch (error: any) {
      this.tableParams.tableLoading = false;
      this.tableData = [];
      this.tableParams.total = 0;
      this.message.error(error);
    }
  }

  // 复制
  copyText(str: string) {
    this.clipboard.copy(str);
    this.message.success('已经成功复制到剪切板');
  }

  /**
   * 当前页码改变时的回调函数
   * @param index
   */
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadFollowUpTable();
  }
  /**
   * 每页条数改变的回调
   * @param sizeNum
   */
  onPageSizeChange(sizeNum: number) {
    this.tableParams.pageSize = sizeNum;
    this.loadFollowUpTable();
  }
}
